package org.example.controller;

import org.example.mapper.FruitMapper;
import org.example.domain.Fruit;
import org.example.response.ApiResponse;
import org.example.service.FruitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 水果控制器
 */
@RestController
@RequestMapping("/api/fruits")
public class FruitController {

    @Autowired
    private FruitService fruitService;
    @Autowired
    private FruitMapper fruitMapper;

    /**
     * 获取所有水果
     * @return 水果列表
     */
    @GetMapping("/getAllFruit")
    public ApiResponse<List<Fruit>> getAllFruits() {
        List<Fruit> fruits = fruitService.getAllFruits();
        return ApiResponse.success(fruits);
    }

    /**
     * 获取所有水果名称列表
     * @return 水果列表
     */
    @GetMapping("/list")
    public ApiResponse<List<Fruit>> List() {
        List<Fruit> fruits = fruitService.list();
        return ApiResponse.success(fruits);
    }

    /**
     * 根据ID获取水果
     * @param id 水果ID
     * @return 水果对象
     */
    @GetMapping("/{id}")
    public ApiResponse<Fruit> getFruitById(@PathVariable Long id) {
        Fruit fruit = fruitService.getFruitById(id);
        if (fruit == null) {
            return ApiResponse.error(404, "水果不存在");
        }
        return ApiResponse.success(fruit);
    }

    /**
     * 添加水果
     * @param fruit 水果对象
     * @param imageFile 图片文件
     * @return 添加的水果对象
     */
    @PostMapping
    public ApiResponse<Fruit> addFruit(@RequestPart("fruit") Fruit fruit, 
                                 @RequestPart(value = "image", required = false) MultipartFile imageFile) {
        try {
            Fruit savedFruit = fruitService.addFruit(fruit, imageFile);
            return ApiResponse.success(savedFruit);
        } catch (Exception e) {
            return ApiResponse.error(500, "添加水果失败: " + e.getMessage());
        }
    }

    /**
     * 更新水果
     * @param id 水果ID
     * @param fruit 水果对象
     * @param imageFile 图片文件
     * @return 更新后的水果对象
     */
    @PutMapping("/{id}")
    public ApiResponse<Fruit> updateFruit(@PathVariable Long id, 
                                    @RequestPart("fruit") Fruit fruit,
                                    @RequestPart(value = "image", required = false) MultipartFile imageFile) {
        try {
            fruit.setId(id);
            Fruit updatedFruit = fruitService.updateFruit(fruit, imageFile);
            return ApiResponse.success(updatedFruit);
        } catch (Exception e) {
            return ApiResponse.error(500, "更新水果失败: " + e.getMessage());
        }
    }

    /**
     * 删除水果
     * @param id 水果ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public ApiResponse<Void> deleteFruit(@PathVariable Long id) {
        try {
            fruitService.deleteFruit(id);
            return ApiResponse.success(null);
        } catch (Exception e) {
            return ApiResponse.error(500, "删除水果失败: " + e.getMessage());
        }
    }


    /**
     * 查看是否有编辑按钮所有权限
     * @return
     */
    @GetMapping("/edit-permission/{id}")
    public ApiResponse<Integer> editPermission(@PathVariable("id") Integer id) {
        Integer flag = fruitService.editPermission(id);
        return ApiResponse.success(flag);
    }

    /**
     * 上架,下架水果
     * @return
     */
    @PostMapping("/status/{id}")
    public ApiResponse<Integer> status(@PathVariable("id") Integer id,@RequestBody Map<String,String> map) {
        Integer flag = fruitService.status(id,map.get("type"));
        return ApiResponse.success(flag);
    }
} 